package com.bl.weilan.service.biz.tradepolicy.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.InstitutionEntity;
import com.bl.weilan.dao.model.TradeChartEntity;
import com.bl.weilan.dao.model.TradePolicyEntity;
import com.bl.weilan.dao.repository.ReportRepository;
import com.bl.weilan.dao.repository.TradeChartRepository;
import com.bl.weilan.dao.repository.TradePolicyRepository;
import com.bl.weilan.service.biz.tradepolicy.TradePolicyService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.institution.InstitutionAddVo;
import com.bl.weilan.service.vo.institution.InstitutionModVo;
import com.bl.weilan.service.vo.tradepolicy.TradePolicyAddVo;
import com.bl.weilan.service.vo.tradepolicy.TradePolicyListVo;
import com.bl.weilan.service.vo.tradepolicy.TradePolicyModVo;
import com.bl.weilan.service.vo.tradepolicy.TradePolicyVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class TradePolicyServiceImpl implements TradePolicyService {
    @Autowired
    TradePolicyRepository tradePolicyRepository;
    @Autowired
    TradeChartRepository tradeChartRepository;

    @Override
    public TradePolicyListVo getList(String id, String order, Pageable pageable){
        List<TradePolicyVo> tradePolicyVos = new ArrayList<>();
        Page<TradePolicyEntity> tradePolicyEntities = null;
        TradePolicyListVo tradePolicyListVo = new TradePolicyListVo();
        if(order.equals("desc")) {
            tradePolicyEntities = tradePolicyRepository.findByTradeIdOrderByTimeDesc(id,pageable);
        } else {
            tradePolicyEntities = tradePolicyRepository.findByTradeIdOrderByTimeAsc(id,pageable);
        }

        if(tradePolicyEntities!=null){
            for (TradePolicyEntity item : tradePolicyEntities){
                TradePolicyVo tradePolicyVo = new TradePolicyVo();
                String tradeid = item.getTradeId();

                BeanUtils.copyProperties(item, tradePolicyVo);
                tradePolicyVos.add(tradePolicyVo);
            }
        }

        tradePolicyListVo.setTotalCount(tradePolicyVos.size());
        tradePolicyListVo.setList(tradePolicyVos);

        return tradePolicyListVo;
    }



    @Override
    public TradePolicyListVo getListManager(String id, Pageable pageable){
        List<TradePolicyVo> tradePolicyVos = new ArrayList<>();
        Page<TradePolicyEntity> tradePolicyEntities = null;
        TradePolicyListVo tradePolicyListVo = new TradePolicyListVo();
        tradePolicyEntities = tradePolicyRepository.findByTradeIdOrderByTimeDesc(id,pageable);
        if(tradePolicyEntities!=null){
            for (TradePolicyEntity item : tradePolicyEntities){
                TradePolicyVo tradePolicyVo = new TradePolicyVo();
                String tradeid = item.getTradeId();

                BeanUtils.copyProperties(item, tradePolicyVo);
                tradePolicyVos.add(tradePolicyVo);
            }
        }

        tradePolicyListVo.setTotalCount(tradePolicyVos.size());
        tradePolicyListVo.setList(tradePolicyVos);

        return tradePolicyListVo;
    }

    @Override
    public void add(TradePolicyAddVo tradePolicyAddVo) {
        TradePolicyEntity tradePolicyEntity = new TradePolicyEntity();
        tradePolicyEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(tradePolicyAddVo, tradePolicyEntity);

        tradePolicyRepository.save(tradePolicyEntity);
    }

    @Override
    public void modify(TradePolicyModVo tradePolicyModVo){
        TradePolicyEntity tradePolicyEntity = tradePolicyRepository.findOne(tradePolicyModVo.getId());

        if (tradePolicyEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        BeanUtils.copyProperties(tradePolicyModVo, tradePolicyEntity);
        tradePolicyRepository.save(tradePolicyEntity);
    }

    @Override
    public void del(DelBody id) {
        tradePolicyRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            tradePolicyRepository.delete(id);
        }
    }

}
